package com.ueboot.weishop.api.mq.config;

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;


/**
 * 启动时初始化MQ 队列名称
 * @author yangkui
 */
@Configuration
public class RabbitConfig {

    @Bean
    public Queue orderSubmit() {
        return new Queue(QueuesType.ORDER_SUBMIT_SYNC);
    }


    @Bean
    public Queue orderSave() {
        return new Queue(QueuesType.ORDER_SAVE_SYNC);
    }

    @Bean
    public Queue orderPayedNotice() {
        return new Queue(QueuesType.ORDER_PAYED_NOTICE);
    }
    @Bean
    public Queue couponSave() {
        return new Queue(QueuesType.COUPON_SAVE_SYNC);
    }

    /**
     * 订单提交队列（需要ACK回答）
     * @param configurer
     * @param connectionFactory
     * @return
     */
    @Bean("submitOrderTaskContainerFactory")
    public SimpleRabbitListenerContainerFactory submitOrderTaskContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setPrefetchCount(1);
        factory.setConcurrentConsumers(1);
        configurer.configure(factory, connectionFactory);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return factory;
    }

    /**
     * 订单保存队列（需要ACK回答）
     * @param configurer
     * @param connectionFactory
     * @return
     */
    @Bean("saveOrderTaskContainerFactory")
    public SimpleRabbitListenerContainerFactory saveOrderTaskContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
                                                                              ConnectionFactory connectionFactory, PlatformTransactionManager transactionManager) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setPrefetchCount(1);
        factory.setConcurrentConsumers(30);
        factory.setTransactionManager(transactionManager);
        configurer.configure(factory, connectionFactory);
        return factory;
    }


}
